/*
 * Droplet.cpp
 *
 * Created: 5/11/2012 4:13:57 PM
 *  Author: common
 */ 

#include <avr/io.h>
#include <math.h>
#include <util/delay.h>

#include "motor.h"
#include "RGB_LED.h"
#include "pc_com.h"
#include "RGB_sensor.h"
#include "power.h"

void Config32MHzClock(void)
{
	// initialize clock source to be 32MHz internal oscillator (no PLL)
	CCP = CCP_IOREG_gc;							//Security Signature to modify clock
	OSC.CTRL = OSC_RC32MEN_bm;					// enable internal 32MHz oscillator
	while(!(OSC.STATUS & OSC_RC32MRDY_bm));		// wait for oscillator ready
	CCP = CCP_IOREG_gc;							//Security Signature to modify clock
	CLK.CTRL = 0x01;							//select sysclock 32MHz osc
}

int main(void)
{
	motor_init();
	RGB_LED_init();
	pc_com_init();
	
	cap_monitor_init();
	
	char i = 0;
	
	printf("Press any key to pause/unpause. \r\n");
	while (1)
	{
		if (PC_USART.STATUS & USART_RXCIF_bm)
		{
			i = PC_USART.DATA;
			printf("\r\nPaused (%c) \r\n", i);
			while (!(PC_USART.STATUS & USART_RXCIF_bm));	
			i = PC_USART.DATA;
		}
	
		printf("%c  ", ++i % 26 + 'A');
		
		printf("CAP STATUS %X\r\n", ACB.STATUS);
		
		if ((ACB.STATUS & AC_WSTATE_gm) == AC_WSTATE_ABOVE_gc) set_blue_led(100);
		if ((ACB.STATUS & AC_WSTATE_gm) == AC_WSTATE_INSIDE_gc)	set_green_led(100);
		if ((ACB.STATUS & AC_WSTATE_gm) == AC_WSTATE_BELOW_gc) set_red_led(100);
		
		_delay_ms(100);
		
		led_off();
		
		_delay_ms(500);
	}	
}